<template>
  <div>
    <a-card :bordered="false" class="ant-pro-components-tag-select" v-show="!hiddenSearch">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="28">
            <a-col :md="5" :sm="12">
              <a-form-item label="号牌种类">
                <a-select placeholder="请选择号牌种类" v-model="queryParam.plateCode" @change="handlePlateCodeChange">
                  <a-select-option v-for="(action) in dictDataPlateType" :key="action.dictValue">
                    {{ action.dictLabel }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="5" :sm="12">
              <a-form-item label="号牌颜色">
                <a-select placeholder="请选择号牌颜色" v-model="queryParam.plateColor" @change="handlePlateColorChange">
                  <a-select-option v-for="(action) in dictDataPlateColor" :key="action.dictLabel">
                    {{ action.dictLabel }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="5" :sm="12">
              <a-form-item label="号牌关键字">
                <a-input v-model="queryParam.keyword" placeholder="请输入号牌关键字"/>
              </a-form-item>
            </a-col>
            <a-col :md="1" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-tooltip placement="topLeft" title="查询" arrow-point-at-center>
                  <a-button shape="circle" icon="search" @click="$refs.table.refresh(true)"/>
                </a-tooltip>
                <a-tooltip placement="topLeft" title="重置" arrow-point-at-center>
                  <a-button style="margin-left: 8px" @click="() => this.queryParam = {}" icon="sync"/>
                </a-tooltip>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card
      style="margin-top: 10px"
      :bordered="false">
      <div class="table-operator">
        <a-tooltip placement="topLeft" title="隐藏/显示搜索栏" arrow-point-at-center>
          <a-button size="small" style="position: absolute;right: 55px;top: 30px;" shape="circle" :icon="icon" @click="hiddenSearchMenu(hiddenSearch)"/>
        </a-tooltip>
        <a-button v-show="addRuleEnable" type="primary" shape="round" icon="plus-circle" @click="$refs.modal.add()">新增</a-button>
        <a-button v-show="removeEnable && selectedRowKeys.length > 0" type="danger" shape="round" icon="delete" @click="onClick(selectedRowKeys)">删除</a-button>
      </div>
      <s-table
        ref="table"
        row-key="id"
        size="middle"
        :columns="columns"
        :data="loadData"
        :rowSelection="removeEnable ? {selectedRowKeys: selectedRowKeys, onChange: onSelectChange} : null"
        showPagination="auto"
        :showTotal="true"
      >
        <span slot="action" slot-scope="text, record">
          <template>
            <a-dropdown :trigger="['click']">
              <a-menu slot="overlay" >
                <a-menu-item v-show="editRuleEnable"><a @click="handleEdit(record)"><a-icon type="edit" theme="filled"/> 编辑</a></a-menu-item>
              </a-menu>
              <a-button type="dashed" icon="bars"/>
            </a-dropdown>
          </template>
        </span>
      </s-table>
      <plate-setting-modal ref="modal" @ok="handleOk"/>
    </a-card>
  </div>
</template>

<script>
import { checkPermission } from '@/utils/permissions'
import {
  batchDelPlateRule,
  dictDataList, findPlateRulePage
} from '@/api/system'
import { STable } from '@/components'
import message from 'ant-design-vue/es/message'
import PlateSettingModal from '../modules/PlateSettingModal'
export default {
  components: {
    STable,
    PlateSettingModal
  },
  name: 'LoginLog',
  data () {
    return {
      // 查询参数
      queryParam: {},
      icon: 'eye-invisible',
      hiddenSearch: false,
      removeEnable: checkPermission('setting:plate:remove'),
      addRuleEnable: checkPermission('setting:plate:add'),
      editRuleEnable: checkPermission('setting:plate:edit'),
      selectedRowKeys: [],
      selectedRows: [],
      dictDataPlateType: [],
      dictDataPlateColor: [],
      // 表头
      columns: [
        {
          title: '号牌种类编码',
          dataIndex: 'plateCode',
          align: 'center',
          width: 80
        },
        {
          title: '号牌种类',
          dataIndex: 'plateType',
          align: 'center',
          width: 80
        },
        {
          title: '号牌颜色',
          dataIndex: 'plateColor',
          align: 'center',
          width: 80
        },
        {
          title: '号牌关键字',
          dataIndex: 'keyword',
          align: 'center',
          width: 80
        },
        {
          title: '号牌分类正则',
          dataIndex: 'sortRegular',
          align: 'center',
          width: 300,
          ellipsis: true
        },
        {
          title: '操作',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' },
          align: 'center',
          width: 100
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return findPlateRulePage(Object.assign(parameter, this.queryParam))
          .then(res => {
            return res.data
          })
      }
    }
  },
  created () {
    this.loadDictDataPlateType()
    this.loadDictDataPlateColor()
  },
  methods: {
    handleEdit (record) {
      this.$refs.modal.edit(record)
    },
    handleOk () {
      this.$refs.table.refresh()
    },
    handlePlateCodeChange (value) {
      this.queryParam.plateCode = value
    },
    handlePlateColorChange (value) {
      this.queryParam.plateColor = value
    },
    loadDictDataPlateType () {
      dictDataList({ dictType: 'plate_type' }).then(res => {
        this.dictDataPlateType = res.data
      })
    },
    loadDictDataPlateColor () {
      dictDataList({ dictType: 'plate_color' }).then(res => {
        this.dictDataPlateColor = res.data
      })
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    hiddenSearchMenu (hiddenSearch) {
      this.hiddenSearch = hiddenSearch !== true
      this.icon = this.hiddenSearch === true ? 'eye' : 'eye-invisible'
    },
    onClick (ids) {
      const self = this
      this.$confirm({
        title: '你确定删除吗?',
        content: <div style="color:red;">删除将永久不能恢复</div>,
        onOk () {
          batchDelPlateRule(ids).then(() => {
            self.$refs.table.refresh()
            message.success('操作成功')
            self.selectedRowKeys = []
          })
        },
        onCancel () {
          self.selectedRowKeys = []
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
  /deep/.ant-form-item-control {
    position: relative;
    line-height: 30px;
    zoom: 1;
  }
</style>
